package com.example.goodtrash.Fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodtrash.R;
import com.example.goodtrash.model.TrashNews;
import com.example.goodtrash.news.NewsWeb;
import com.example.goodtrash.search.imageSearch;

import java.util.List;

public class TrashNewsAdapter extends RecyclerView.Adapter<TrashNewsAdapter.ViewHolder>{
    private List<TrashNews> mNewsList;
    private Context mcontext;
    private Activity mActivity;
    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        TextView description;
        ImageView newsImage;
        View newsView;
        public ViewHolder(View view) {
            super(view);
            title=view.findViewById(R.id.tv_title);
            description=view.findViewById(R.id.tv_description);
            newsImage=view.findViewById(R.id.news_image);
            newsView=view;

        }
    }

    public TrashNewsAdapter(List<TrashNews> newsList,Context context,Activity activity){
        mNewsList=newsList;
        mcontext=context;
        mActivity=activity;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news,parent,false);
        TrashNewsAdapter.ViewHolder holder=new TrashNewsAdapter.ViewHolder(view);

        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        TrashNews trashNews=mNewsList.get(position);
        holder.title.setText(trashNews.getTitle());
        holder.description.setText(trashNews.getDescription());
        Glide.with(mActivity).load(trashNews.getPicUrl()).into(holder.newsImage);
        holder.newsView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(mcontext, NewsWeb.class);
                intent.putExtra("Url",trashNews.getUrl());
                mActivity.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mNewsList.size();
    }
}
